package com.apes.systemMonitoring;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.exception.MessageSubscriber;
import com.apes.exception.MessageSubscriberRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.outbox.OutboxEvent;
import com.apes.framework.plugin.outbox.OutboxEventRepository;
import com.apes.framework.plugin.outbox.OutboxSubscriberManager;
import com.apes.framework.plugin.soe.metadata.BusinessType;
import com.apes.framework.plugin.soe.store.database.service.BusinessTypeService;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Title: SystemMonitoringService
 * @Description: 系统监控
 * @author: ZhengZeXian
 * @create: 2021/5/6 14:46
 */
@Service("systemMonitoringService")
public class SystemMonitoringService extends DomainService {

    @Autowired
    private OutboxEventRepository outboxEventRepository;
    @Autowired
    private MessageSubscriberRepository messageSubscriberRepository;
    @Autowired
    private OutboxSubscriberManager outboxSubscriberManager;

    public List<Map> findAll(SimpleRequest request){
        MessageSubscriber messageSubscriber = messageSubscriberRepository.findOne("000040");
        List<Map> returnData = new ArrayList<>();
        if(!messageSubscriber.getMessageTopics().isEmpty()){
            messageSubscriber.getMessageTopics().forEach(subscriber ->{
                List<OutboxEvent> outboxEvents = SpringManager.getBean(OutboxEventRepository.class).findAll(
                        JpaDsl.toCriteria("subscriber","EQ",subscriber.getTopic())
                );
                if (!outboxEvents.isEmpty()){
                    returnData.add(MapUtil.mapper(
                            "subscriber",outboxEvents.get(0).getSubscriber(),
                            "LEFTTEXT","单据类型：" + businessType(outboxEvents.get(0).getAggregateType())
                                    + " [" + outboxEvents.get(0).getAggregateType() + "]",
                            "RIGHTTEXT",outboxEvents.size()
                    ));
                }
            });
        }
        return returnData;
    }

    /**
     * 根据订阅者ID 查找数据
     * @param request
     * @return
     */
    public Map findBySubscriber(SimpleRequest request){
        JSONObject param = request.getJO();
        JSONObject data = param.getJSONObject("data"); // 表单数据
        String subscriber = data.getString("subscriber");
        List<OutboxEvent> outboxEvents = outboxEventRepository.findBySubscriberOrderByCreateDateAsc(subscriber);
        List<Map> maps = MapUtil.mapped(outboxEvents);
        JSONArray objects = toJSONArray(maps);
        return MapUtil.mapper(
                "content", objects,
                "total", objects.size(),
                "size",  objects.size()
        );
    }

    /***
     * 功能： 用于处理返回值
     * @param lists
     * @return
     */
    protected JSONArray toJSONArray(List<Map> lists) {
        JSONArray returnArr = new JSONArray();
        lists.stream().forEach(list -> returnArr.add(new JSONObject(list)));
        return returnArr;
    }

    @SneakyThrows
    private String businessType(String aggregateType) {
        Class clazz = Class.forName(aggregateType);
        return SpringManager.getBean(BusinessTypeService.class).getBusinessTypeByCache(clazz.getSimpleName()) == null ? clazz.getSimpleName() : SpringManager.getBean(BusinessTypeService.class).getBusinessTypeByCache(clazz.getSimpleName()).getName();
    }
}
